﻿////////////////////////////////////////////////////////////////////////////////
//
// Ariaware Source Code - Confidential Material
// Copyright © 2004 Aral Balkan. All Rights Reserved.
// Copyright © 2004 Ariaware Limited.
//
// Project: ARP Pizza Sample Application
// File: com/ariaware/pizza/business/ServiceLocator.as
// Created by: Aral Balkan
//
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Imports
////////////////////////////////////////////////////////////////////////////////

import mx.remoting.Service;

// debug
import mx.remoting.debug.NetDebug;

import com.ariaware.arp.ServiceLocatorTemplate;

////////////////////////////////////////////////////////////////////////////////
//
//
// Class: ServiceLocator (Singleton)
//
//
////////////////////////////////////////////////////////////////////////////////
class com.ariaware.pizza.business.ServiceLocator extends com.ariaware.arp.ServiceLocatorTemplate
{
	//
	// Properties
	//
	private static var inst;		// instance of the Service Locator singleton
	
	// Inherited from template:
	// serviceRegistry (private) - registry of known services
	
	////////////////////////////////////////////////////////////////////////////
	//
	// Constructor
	//
	////////////////////////////////////////////////////////////////////////////
	function ServiceLocator ()
	{
		super();
	
		// debug
		NetDebug.initialize();
	}
	
	////////////////////////////////////////////////////////////////////////////
	//
	// Public Methods
	//
	////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////
	//
	// getInstance()
	//
	// The ServiceLocator is a singleton and this is its accessor method.
	//
	////////////////////////////////////////////////////////////////////////////
	public static function getInstance ()
	{
		//
		// Return reference to singleton instance
		//
		if ( inst == null )
		{
			// create a single instance of the singleton
			inst = new ServiceLocator();
			return inst;
		}
		else
		{
			// instance already exists, return a reference to it
			return inst;
		}
	}

	////////////////////////////////////////////////////////////////////////////
	// 
	// getService() 
	//
	// Override the getService() concrete operation in the superclass, 
	// treating it as if it was a hook operation for the purpose of adding
	// some additional run-time validation (to check for serviceType.)
	//
	////////////////////////////////////////////////////////////////////////////
	public function getService ( serviceName )
	{
		// Get the service instance 
		var theService = super.getService ( serviceName );

		//
		// Do some additional validation that is specific to our application.
		//
		if ( theService instanceof mx.remoting.Service )
		{
			return theService;
		}
		else
		{
			trace ("Service Locator Error: Unknown service type requested - "+serviceName);
			trace ("Currently this application can only handle Remoting services.");
		}
	}

	////////////////////////////////////////////////////////////////////////////
	//
	// Private Methods
	//
	////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////
	//
	// addServices()
	//
	// Override the addServices() primitive operation in the superclass to 
	// add the services used by this application.
	//
	////////////////////////////////////////////////////////////////////////////
	private function addServices ()
	{
		//
		// Add your services here
		//
		
		//
		// Site Service
		// 
		var gatewayURL:String = "http://www.amfphp.org/examples/pizzaservice/gateway.php";
		var pizzaService:Service = new Service ( gatewayURL, null, "pizzaService", null, null );
		addService ( "pizzaService", pizzaService );
	}
}	
